package com.cbank.dao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import com.cbank.domain.UserCompany;
@Repository
public interface UserCompanyDao {
	public final static String ALL = "id,view_id,user_id,phone_num,status,update_time,create_time,company_table_id";

	@Insert("insert into user_company(view_id,user_id,phone_num,status,company_table_id) values(#{viewId},#{userId},#{phoneNum},#{status},#{companyTableId})")
	@Options(useGeneratedKeys = true)
    void insert(UserCompany obj);
	
	@Select("select " + ALL + " from user_company where id = #{id} and status = 1 limit 1")
    UserCompany getById(Long id);

	@Select("select " + ALL + " from user_company where status = 1 order by create_time DESC limit #{index},#{offset}")
    List<UserCompany> getPage(@Param(value = "index") int index, @Param(value = "offset") int offset);
	
	@Select("select " + ALL + " from user_company where status = 1 order by create_time DESC limit #{index},#{offset}")
    List<UserCompany> getPageWithParam(@Param(value = "index") int index, @Param(value = "offset") int offset);
	

	@Select("select count(0) from user_company where status = 1")
	public int getCount();
	
	/**
	 * 得到总共下载数量
	 * @param userId
	 * @return
	 */
	@Select("select count(0) from user_company where status = 1 and user_id = #{userId} and to_days(update_time) = to_days(now())")
	public int getTotalCountByUserId(long userId);
	/**
	 * 得到总共下载数量
	 * @param userId
	 * @return
	 */
	@Select("select "+ALL+" from user_company where user_id = #{userId} and to_days(update_time) = to_days(now())")
	public List<UserCompany> getListByUserId(long userId);
	
	
}
